package com.swpu.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swpu.commonutils.R;
import com.swpu.eduservice.entity.EduCourse;
import com.swpu.eduservice.entity.EduTeacher;
import com.swpu.eduservice.service.EduCourseService;
import com.swpu.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 功能描述：
 *
 * @Author: ygq
 * @Date: 2021/10/22 20:59
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/index")
public class IndexController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService teacherService;

    //查询前8条热门课程，查询前4条名师
    @GetMapping("getAll")
    public R index() {
        //查询前8条热门课程
//        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
//        wrapper.orderByDesc("id");
//        wrapper.last("limit 8");
//        List<EduCourse> eduList = courseService.list(wrapper);
        //查询前4条名师
//        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
//        wrapperTeacher.orderByDesc("id");
//        wrapperTeacher.last("limit 4");
//        List<EduTeacher> teacherList = teacherService.list(wrapperTeacher);
        List<EduCourse> eduList =  courseService.getCourseList();
        List<EduTeacher> teacherList = teacherService.getTeacherList();
        return R.ok().data("eduList",eduList).data("teacherList",teacherList);
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        Map<String, Object> data = teacherService.pageListWeb(pageParam);

        return  R.ok().data(data);
    }

    @GetMapping("getById/{teacherId}")
    public R getCourseAndTeById(@PathVariable String teacherId){
        EduTeacher byId = teacherService.getById(teacherId);
        QueryWrapper<EduCourse> qw = new QueryWrapper<>();
        qw.eq("teacher_id",teacherId);
        List<EduCourse> list = courseService.list(qw);
        return R.ok().data("list",list).data("teacher",byId);
    }




}
